package com.paranoidvulture.javaEngine.Views;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.util.ArrayList;

public class ViewHandler {
	ArrayList<IView> l;
	IView view;
	private int nestedLevel = 0;
	private ViewBase vB;
	
	//constructor
	public ViewHandler(){
		this.l = new ArrayList<IView>();
	}
	
	//count
	public int count(){
		if(l.isEmpty()){
			return 0;
		}else {
			return l.size();
		}
	}
	
	//get last View add to list
	public IView getTopView(){
		if(!this.l.isEmpty()){
			int i = this.l.size();
			return this.l.get(i-1);
		}else{
			return null;
		}
	}
	
	//add to list
	public void addToList(IView view){
		
		if (!l.isEmpty()){
			view.setEnabled(true);
			this.view = getTopView();
			this.view.setEnabled(false);
			l.set(l.size() - 1, this.view);
			l.add(view);
		}else if(l.isEmpty()){
			view.setEnabled(true);
			l.add(view);
		}
	}
	
	public void removeTopView(){
		if(!l.isEmpty()){
			ArrayList<IView> temp = new ArrayList<IView>();
			this.view = getTopView();
			this.view.setEnabled(false);
			for (int i = 0; i <= l.size() - 2; i++){
				temp.add(l.get(i));
			}
			this.l.clear();
			this.l = temp;
			this.view = getTopView();
			this.view.setEnabled(true);
			this.l.set(this.l.size()-1, this.view);
		}
	}
	
	//update top view
	public void updateCurrentView(long timePassed){
		if(!l.isEmpty()){
			int childLevel = 0;
			getTopView().update(timePassed);
			getNested(this);
			vB = (ViewBase) this.getTopView();
			childLevel = getNested(vB.views);
			nestedLevel += childLevel;
		}
		
	}
	
	//if count is more than or equal to 1 return 2
	//if count is 0 return 1
	public int getNested(ViewHandler vH){
		if (vH != null){
			if (vH.count() == 1){
				nestedLevel = 1;
			}else if (vH.count() == 2){
				nestedLevel = 2;
			}else {
				nestedLevel = 3;
			}
		}else nestedLevel = 1;
		
		return nestedLevel;
	}
	
	//get the nested level of the stack
	public int getNestedLevel(){
		return nestedLevel;
	}
	
	//draw top view
	public void drawCurrentView(Graphics2D g){
		if(!l.isEmpty()){
			getTopView().draw(g);
		}
	}
	
	//update button press for top view
	public void updateKeyEventPressed(KeyEvent e){
		if(!l.isEmpty()){
			getTopView().keyPressed(e);
		}
	}
	
	//update button released for top view
	public void updateKeyEventReleased(KeyEvent e){
		if(!l.isEmpty()){
			getTopView().keyReleased(e);
		}
	}
	
	//update button typed or something or another
	public void updateKeyEventTyped(KeyEvent e){
		if(!l.isEmpty()){
			getTopView().keyTyped(e);
		}
	}
	
	public void updateMouseClicked(MouseEvent e) {		
		if(!l.isEmpty()){
			getTopView().mouseClicked(e);
		}
	}

	
	public void updateMouseEntered(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseEntered(e);
		}
	}

	
	public void updateMouseExited(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseExited(e);
		}
	}

	
	public void updateMousePressed(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mousePressed(e);
		}
	}

	
	public void updateMouseReleased(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseReleased(e);
		}
	}

	
	public void updateMouseDragged(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseDragged(e);
		}
	}

	
	public void updateMouseMoved(MouseEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseMoved(e);
		}		
	}

	
	public void updateMouseWheelMoved(MouseWheelEvent e) {
		if(!l.isEmpty()){
			getTopView().mouseWheelMoved(e);
		}
	}
}
